package com.yjhb.android.sbz.fragment.tipoff;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.adapter.TipOffAdapter;
import com.yjhb.android.sbz.dialog.TipOffDialog;
import com.yjhb.android.sbz.dialog.TipsDialog;
import com.yjhb.android.sbz.entity.Report;
import com.yjhb.android.sbz.fragment.BaseListFragment;
import com.yjhb.android.sbz.manager.ResManager;
import com.yjhb.android.sbz.mvp.tipofflist.TipOffListPresenter;
import com.yjhb.android.sbz.mvp.tipofflist.TipOffListView;
import com.yjhb.android.sbz.tool.Tool;
import com.yjhb.android.sbz.v2.activity.ReportContentActivityV2;
import com.jojo.android.zxlib.tool.ZxToast;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.io.Serializable;
import java.util.List;

/**
 * 创建者 zx
 * 创建时间 2020/12/22 15:29
 */
public class TipOffFragment extends BaseListFragment<TipOffListPresenter, TipOffAdapter, Report> implements TipOffListView {
    private final String TAG = "TipOffFragment";

    boolean isLaunch;


    @Override
    protected TipOffAdapter getAdapter() {
        return new TipOffAdapter(getContext(), isLaunch);
    }

    @Override
    protected CharSequence getTips() {
        if (isLaunch) {
            return null;
        }
        return "24小时不辩诉将判对方胜";
    }

    @Override
    protected void initUI() {
        super.initUI();
        SmartRefreshLayout.LayoutParams params = (SmartRefreshLayout.LayoutParams) content_layout.getLayoutParams();
        params.rightMargin = ResManager.dp() * 15;
        params.leftMargin = ResManager.dp() * 15;
        if (isLaunch)
            params.topMargin = ResManager.dp() * 15;
        params.bottomMargin = ResManager.dp() * 15;
//        content_layout.setBackgroundResource(R.drawable.corners_white);
        tips_tv.setTextColor(getResources().getColor(R.color.red_00));
    }

    @Override
    protected void onLoadData(int size, int current) {
        presenter.getReportList(isLaunch, size, current);
    }

    @Override
    protected TipOffListPresenter initPresenter() {
        return new TipOffListPresenter(this);
    }

    @Override
    public void onItemClick(int index, Object o) {

    }

    @Override
    public void onViewClick(View view, String msg, int index, Object o) {
        Report report = (Report) o;
        if (msg.equals("lookOver")) {
//            Intent intent = new Intent(getContext(), ReportContentActivity.class);
            Intent intent = new Intent(getContext(), ReportContentActivityV2.class);
            intent.putExtra("report", (Serializable) o);
            startActivity(intent);
        } else if (msg.equals("plea")) {
            TipsDialog tipsDialog = new TipsDialog(getContext());
            tipsDialog.setTitle("提示");
            tipsDialog.setContent("若确实存在审核/提交过错，可先与对方沟通撤销举报。\n" +
                    "一旦辩诉，无法撤销，是否辩诉？");
            tipsDialog.setSubmitText("我要辩诉");
            tipsDialog.setCancelText("取消");
            tipsDialog.setContentGravity(Gravity.CENTER);
            tipsDialog.setOnClickListener(new TipsDialog.OnClickListener() {
                @Override
                public void onClick(int type) {
                    tipsDialog.cancel();
                    if (type == TipsDialog.TYPE_SUBMIT) {
                        TipOffDialog offDialog = new TipOffDialog((Activity) getContext(), (Report) o);
                        offDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                            @Override
                            public void onDismiss(DialogInterface dialog) {
                                current = 1;
                                onLoadData(size, current);
                            }
                        });
                        offDialog.show();
                    }
                }
            });
            tipsDialog.show();
        } else if (msg.equals("撤销举报")) {
            TipsDialog tipsDialog = new TipsDialog(getContext());
            tipsDialog.setTitle("提示");
            tipsDialog.setContent("是否要撤销举报？");
            tipsDialog.setSubmitText("确定撤销");
            tipsDialog.setCancelText("取消");
            tipsDialog.setContentGravity(Gravity.CENTER);
            tipsDialog.setOnClickListener(new TipsDialog.OnClickListener() {
                @Override
                public void onClick(int type) {
                    tipsDialog.cancel();
                    if (type == TipsDialog.TYPE_SUBMIT) {
                        presenter.cancelReport(report.getId());
                    }
                }
            });
            tipsDialog.show();
        } else if (msg.equals("联系举报人")) {
            Tool.startChatActivity(getContext(), "" + report.getPlaintiff(), report.getPlaintiffName(), 4);
        } else if (msg.equals("通过任务")) {
            presenter.passTask(report.getId());
        } else if (msg.equals("continue")) {
            TipOffDialog offDialog = new TipOffDialog((Activity) getContext(), (Report) o);
            offDialog.show();
        }
    }

    public void setLaunch(boolean launch) {
        isLaunch = launch;
    }

    @Override
    public void onWait() {
        loadingDialog.show();
    }

    @Override
    public void onCancel() {
        loadingDialog.cancel();
        ZxToast.showSuccessCenterToast("撤销成功！");
        onLoadData(size, current);
    }

    @Override
    public void onTaskPass() {
        loadingDialog.cancel();
        ZxToast.showINFOBottomToast("已通过该举报相关任务");
        onLoadData(size, current);
    }

    @Override
    public void onLoad(List<Report> data) {
        smartRefreshLayout.finishLoadmore();
        this.dataSet.addAll(data);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onRefresh(List<Report> data) {
        smartRefreshLayout.finishRefresh();
        loadingDialog.cancel();
        this.dataSet.clear();
        this.dataSet.addAll(data);
        adapter.notifyDataSetChanged();
    }

//    @Override
//    public void onFailure(int code, String msg) {
//        loadingDialog.cancel();
//        smartRefreshLayout.finishRefresh();
//        smartRefreshLayout.finishLoadmore();
//    }
}
